AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.damaged = 0
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Outputs = Wire_CreateOutputs(self.Entity, { "Beers", "Max Beers" })
		self.Inputs = Wire_CreateInputs(self.Entity, { "Dispense" })
	end
end

function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.health = self.maxhealth
	self.damaged = 0
end

function ENT:Destruct()
	LS_Destruct( self.Entity, true )
end

function ENT:Leak()
end

function ENT:UpdateMass()
	//change mass
  local units = RD_GetResourceAmount(self, "beers") * RD_GetUnitCapacity(self, "beers") / RD_GetNetworkCapacity(self, "beers")
	local mass = self.mass + units*1 // self.mass = default mass
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		if phys:GetMass() != mass then
			phys:SetMass(mass)
			phys:Wake()
		end
	end
end

function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
    if (RD_GetResourceAmount(self, "beers") >= 1) then
      self:DropBeer()
    end
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "Dispense") and (value ~= 0) and (RD_GetResourceAmount(self, "beers") >= 1) then
    self:DropBeer()
  end
end

function ENT:DropBeer()
  RD_ConsumeResource(self, "beers", 1)
  local ent = ents.Create( "beer_bottle" )
  	ent:SetPos( self.Entity:GetPos() +  self.Entity:GetUp() * 5)
  ent:Spawn()
  ent:Activate()
  ent.beers = 1 -- We set this here instead of the Initialize function to prevent people from duping beer like cheaters
end

function ENT:Think()
	self.BaseClass.Think(self)
	
	--[[if (self.damaged == 1) then
		--self:Leak()
	end]]--
	
	if not (WireAddon == nil) then
		self:UpdateWireOutput()
	end
  
	self:UpdateMass()
	
	self:NextThink( CurTime() +  1 )
	return true
end

function ENT:UpdateWireOutput()
	local beers = RD_GetResourceAmount(self, "beers")
	local maxbeers = RD_GetNetworkCapacity(self, "beers")
	Wire_TriggerOutput(self.Entity, "beers", beers)
	Wire_TriggerOutput(self.Entity, "Max beers", maxbeers)
end
